<?php
session_start();
include "../include/db.php";
include "../include/head.php";
require("../include/pop3.php");
require("../include/class.matchall.php");
require("../include/class.phpmailer.php");
?>
<table width="100%">
    <tr>
    	<td width="200" valign="top">
          Administrator option
          <UL>
           <li><a href="allcompanies.htm">All companies</a></li>
           <li><a href="report.htm">Report</a></li>
           <li><a href="scanemail.php">Scan Email</a></li>
          </UL>
   	  </td>   
   	  <td valign="top">
   	  some thing abount administrator ............................</br>
<?
function saveemail($conn,$from,$to,$subject,$body)
{
    $sql = "insert into mails(fromemail,toemail,subject,body)
         values('".($from)."','".($to)."','".($subject)."','".($body)."')"; 
    $res = mysql_query($sql, $conn);
    $err = mysql_error();
    if($err)
    {
        echo "insert record into database".$err;
    }
    else
    {
        echo "insert email success";
    }
}
function receiveemail($conn)
{
    $user="admin@uwannamail.com";
    $password="ByronStraat9";
    $apop=0;
    $pop3_connection=new pop3_class;
    $pop3_connection->hostname="mail.uwannamail.com";
    if(($error=$pop3_connection->Open())=="")
    {
        if(($error=$pop3_connection->Login($user,$password,$apop))=="")
        {
            if(($error=$pop3_connection->Statistics(&$messages,&$size))=="")
            {
                $result=$pop3_connection->ListMessages("",0);
                if(GetType($result)=="array")
                {
                    for(Reset($result),$message=0;$message<count($result);Next($result),$message++)
                    {
                        if(GetType($result)=="array")
                        {
                            if(($error=$pop3_connection->RetrieveMessage($message+1,&$headers,&$body,2))=="")
                            {
                                $from="";
                                $to="";
                                $subject="";
                                for($line=0;$line<count($headers);$line++)
                                {
                                    if (substr($headers[$line],0,4)=='From')
                                    {
                                        $matchresult= MatchAll::email_match($headers[$line],true,false);
                                        $from=$matchresult[0];
                                    }
                                    if (substr($headers[$line],0,2)=='To')
                                    {
                                        $matchresult= MatchAll::email_match($headers[$line],true,false);
                                        $to=$matchresult[0];
                                    }
                                    if (substr($headers[$line],0,7)=='Subject')
                                    {
                                        $subject=substr($headers[$line],8);
                                    }
                                }
                                $content="";
                                for($line=0;$line<count($body);$line++)
                                {
                                    $content.=$body[$line];
                                }
                                if(($error=$pop3_connection->DeleteMessage($message+1))=="")
                                {
                                    echo "<PRE>Marked message 1 for deletion.</PRE>\n";
                                    if(($error=$pop3_connection->ResetDeletedMessages())=="")
                                    {
                                        echo "<PRE>Resetted the list of messages to be deleted.</PRE>\n";
                                    }
                                }
                                // saveemail($conn,$from,$to,$subject,$content);
                            }
                        }
                        else
                        {
                            $error=$result;
                        }
                    }
                    if($error==""&& ($error=$pop3_connection->Close())=="")
                    {
                        echo "<PRE>Disconnected from the POP3 server &quot;$pop3_connection->hostname&quot;.</PRE>\n";
                    }
                }
                else
                {
                    $error=$result;
                }
            }
        }
    }
    if($error!="")
    {
        echo "<H2>Error: ",HtmlSpecialChars($error),"</H2>";
    }
}
function genforward($conn)
{
    $sql = "select * from mails where forwardpos is null";
    $result=mysql_query($sql,$conn);
    while   ($row=mysql_fetch_array($result))
    {
        $sqll="select distinct membermailing.memberid from mails,mailing,membermailing where mails.fromemail=mailing.fromemail and mailing.id=membermailing.mailingid and mails.id=".$row['id'];
        $result1=mysql_query($sqll,$conn);
        $user="";
        $forwardcount=0;
        while  ($row1=mysql_fetch_array($result1))
        {
            $user.=$row1['memberid'].",";
            $forwardcount++;
        }
        $sqlupdate = "update mails set forward='".$user."',forwardcount=".$forwardcount." where id=".$row['id'];
        mysql_query($sqlupdate,$conn);
    }
}
function sendmail($conn)
{
    $sql = "select * from mails where forward is not null and (forwardpos is null or forwardpos<forwardcount)";
    $result=mysql_query($sql,$conn);
    while   ($row=mysql_fetch_array($result))
    {
        $pos = $row['forwardpos'];
        if ($pos=="")
        {
            $pos=0;
        }
        $pieces = explode(",", $row['forward']);
        $memberid=$pieces[$pos];
        if ($memberid!="")
        {
            //sendmail
            $sqluser = "select * from member where id=".$memberid;
            $resultuser=mysql_query($sqluser,$conn);
            if($rowuser=mysql_fetch_array($resultuser))
            {
                sendmailout($rowuser['email'],$row['subject'],$row['body']);
                $sqlupdate = "update mails set forwardpos=".($pos+1)." where id=".$row['id'];
                $resultabc=mysql_query($sqlupdate,$conn);
            }
        }
    }
}
function sendmailout($email,$subject,$body)
{
    echo "send mail to ".$email;
    $mail = new PHPMailer();

    $mail->IsSMTP();                       // send via SMTP
    $mail->Host     = "mail.uwannamail.com"; // SMTP servers
    $mail->SMTPAuth = true;     // turn on SMTP authentication
    $mail->Username = "admin@uwannamail.com";  // SMTP username
    $mail->Password = "ByronStraat9"; // SMTP password

    $mail->From     = "admin@uwannamail.com";
    $mail->FromName = "admin";
    $mail->AddAddress($email);
    $mail->AddReplyTo("admin@uwannamail.com","Uwannamail Admin");

    $mail->WordWrap = 50;                              // set word wrap
    //$mail->AddAttachment("/var/tmp/file.tar.gz");      // attachment
    //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
    $mail->IsHTML(true);                               // send as HTML

    $mail->Subject  =  $subject;
    $mail->Body     =  $body;
    //$mail->AltBody  =  "This is the text-only body";

    if(!$mail->Send())
    {
        echo "Message was not sent <p>";
        echo "Mailer Error: " . $mail->ErrorInfo;
        exit;
    }

}

receiveemail($conn);
//genforward($conn);
//sendmail($conn);
//sendmailout("richard@wolffoot.com","test subject","test body");
?>

   	  </td>   
   	</tr>
</table>
<?php
include "../include/foot.php";
?>